package com.miukoo.aikao.admin.types.impl;

import com.miukoo.aikao.core.dtos.QuestionAnswerDto;
import com.miukoo.aikao.core.dtos.QuestionDetailDto;
import com.miukoo.aikao.core.exceptions.ApiException;
import com.miukoo.aikao.core.pojos.Question;
import com.miukoo.aikao.core.pojos.QuestionChooseDto;
import com.miukoo.aikao.core.types.TypeInterface;
import com.miukoo.aikao.qmanage.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 问答题的实现类
 * @Author miukoo<miukoo@126.com>
 * @Date 2020/11/25  
 * @Version 1.0
 */
@Component
public class QaType implements TypeInterface {

    @Autowired
    QuestionService questionService;

    @Override
    public void saveCkeck(QuestionDetailDto dto) {
        String no = "【问答题】"+dto.getContent();
        List<QuestionAnswerDto> answerList = dto.getAnswerList();
        if(answerList==null || answerList.size() < 1){
            throw new ApiException(1,no+"至少有一个答案！");
        }
    }

    /**
     * 选择
     * @param repoId        题库
     * @param type          题类型
     * @param level         难度
     * @param size          多少随机题
     * @param answerCount   最多多少个选项
     * @param score   题得分
     * @return
     */
    @Override
    public List<QuestionChooseDto> chooseQuestion(Long repoId, Short type, Short level, Short size, Short answerCount, Short score) {
        List<Question> questions = questionService.listByRandom(repoId, type, level, null, size);
        List<QuestionChooseDto> all = new ArrayList<>();
        if(questions!=null){
            questions.forEach(question->{
                question.setAnswers(null);
                QuestionChooseDto dto = new QuestionChooseDto();
                BeanUtils.copyProperties(question, dto);
                dto.setScore(score);
                all.add(dto);
            });
        }
        return all;
    }

}
